<?php

if(defined("_PARA_LINK_ROLL"))
    return;
define("_PARA_LINK_ROLL", 1);

require_once "LinkRollLink.php";
require_once "LinkRollCategory.php";

class LinkRoll extends HTMLable {
    public $links;
    public $link_count     = 0;
    public $approved_count = 0;
    public $waiting_count  = 0;
    public $newest_id;
    public $categories;

    public function LinkRoll( $links, $categories ) {

        if(is_array( $categories ) ) {
            $this->categories = array();
            foreach($categories as $cat )
                $this->categories[ $cat->id ] = $cat;
        }

        $this->links = $links;
        $this->link_count = sizeof( $links );

        if($this->link_count > 0) {
                $this->newest_id = $this->links[ 0 ]->id;
                foreach( $links as $link)
                    if($link->approved)
                        $this->approved_count++;
                    else
                        $this->waiting_count++;
                
        }
    }


    public function get_category_string( $link ) {
        $cat = $this->categories[ $link->category_id];
        if( $cat )
            return $cat->name;
        return "";
    }


    public function htmlize( $options ) {
        global $parapara;
        $parapara->now;

        if( $options['boxview']) {
            return $this->htmlize_box();
        }

        if( $options['show_form'] ) {
            $this->show_form( $options['formoptions'] );
            return;
        }

        // do the topmenu
        echo "<div id=\"para_linkroll\">\n";
        echo "<div id=\"para_linkroll_ctrls\" >";
        echo "<span id=\"para_linkroll_ctrls_item\" onclick=\"para_linkroll_uev_showform()\">Giv os et fedt link</span>";

        echo "</div>\n";
        
        echo "<div id=\"para_linkroll_links\">\n";

        if(!$this->link_count ) {
            echo "<div class=\"para_linkroll_empty\">Der er ingen links i dag.</div>\n";
        }
        else {
            $curdate = '';

            foreach ( $this->links as $link ) {
                if( $curdate != Helpers::get_date( $link->timestamp ) ) {
                    $curdate = Helpers::get_date( $link->timestamp );
                    echo "<div class='para_linkroll_date'>", Helpers::nice_date( $curdate ), "</div>\n";
                }

                $link->htmlize( array( 
                                    'category' => $this->categories[ $link->category_id ])
                    );
            }
        }

        echo "</div>\n";

        echo "</div>\n";
    }


    private function htmlize_box() {
        if(!$this->link_count ) {
            echo "<div class=\"para_linkrollbox_empty\">Der er ingen links i dag.</div>\n";
        }
        else {
            foreach( $this->links as $link )
                $link->htmlize( 
                    array( 
                        'category' => $this->categories[ $link->category_id ],
                        'boxview'  => true )
                    );
        }
       
        echo "<a id=\"para_linkroll_box_submitlink\" href=\"?sec=linkroll&subsec=linksubmit\">Giv os et fedt link</a>";
    }

    private function cat_select() {
        $chosen = -1000;

        echo "<select name=\"cat_id\" id=\"para_linkroll_input_cat_id\">\n";
        foreach ($this->categories as $cat ) {
            if($chosen == $cat->id)
                $selected = " selected=\"selected\"";
            else
            $selected = '';
        echo "<option value=\"$cat->id\">", 
            htmlspecialchars( $cat->name ),
            "</option>\n";                
        }
        echo "</select>\n";
    }


    private function show_form( $formoptions ) {
        $sf_url      = htmlspecialchars( $formoptions['url'] );
        $sf_headline = htmlspecialchars( $formoptions['headline'] );
        $sf_section  = htmlspecialchars( $formoptions['category'] );

        echo "<div class=\"para_linkroll_form\">\n";

        echo "<div class=\"para_linkroll_form_headline\">Giv os et link</div>\n";

        echo "<form onsubmit=\"return para_linkroll_uev_send_link()\">\n";
        echo "<table>\n";

        echo "<tr>\n";
        echo "<td class=\"para_linkroll_form_label\">URL:</td>\n";
        echo "<td class=\"para_linkroll_form_value\">\n",
            "<input type=\"text\" name=\"url\" value=\"$sf_url\" id=\"para_linkroll_input_url\" size=\"60\"/>",
            "</td>\n";
        echo "</tr>\n";

        echo "<tr>\n";
        echo "<td class=\"para_linkroll_form_label\">Overskrift:</td>\n";
        echo "<td class=\"para_linkroll_form_value\">\n",
            "<input type=\"text\" name=\"headline\" value=\"$sf_headline\" id=\"para_linkroll_input_headline\" size=\"60\"/>",
            "</td>\n";
        echo "</tr>\n";

        echo "<tr>\n";
        echo "<td class=\"para_linkroll_form_label\">Kategori:</td>\n";
        echo "<td class=\"para_linkroll_form_value\">\n",
            $this->cat_select(),
            "</td>\n";
        echo "</tr>\n";


        echo "<tr>\n";
        echo "<td class=\"para_linkroll_formbuttons\" colspan=\"2\">",
            "<button class=\"para_button\" name=\"save\"    type=\"submit\" value=\"save\">Gem</button>",
            "<button class=\"para_button\" name=\"preview\" type=\"submit\" value=\"preview\" onclick=\"return para_linkroll_uev_preview_link()\">Preview</button>",            
            "</td>\n";
        echo "</tr>\n";


        echo "</table>\n";
        echo "</form>\n";


        echo "<div class=\"para_linkroll_form_preview\"></div>\n";

        echo "</div>\n";
   
    }
}

?>
